package com.bwie.bwedu.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;

import com.bwie.bwedu.R;
import com.bwie.bwedu.adapter.Trackadapter;
import com.bwie.bwedu.bean.Student;
import com.bwie.bwedu.presenter.impl.InvalidPresenterImpl;
import com.bwie.bwedu.view.TrackView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/7/21.
 * 无效信息展示
 */
public class InvalidFragment extends BaseFragment implements TrackView {
    private PullToRefreshListView listview;
    private View view;
    private Trackadapter adapter;
    private int pn=1;
    private String type="加载";
    private ArrayList<Student> listall=new ArrayList<Student>();
    private InvalidPresenterImpl presenter;

    @Override
    public View getView(LayoutInflater inflater) {
        view = inflater.inflate(R.layout.fragment_track,null);
        return view;
    }
    @Override
    public void findView() {
        listview = (PullToRefreshListView) view.findViewById(R.id.track_list);
    }

    @Override
    public void setData() {
        presenter = new InvalidPresenterImpl(this);
        presenter.invalid(3+"",pn+"");
    }

    @Override
    public void setListener() {
        //刷新加载
        listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                type="刷新";
                PullDown();
            }


            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                type="加载";
                PullDown();

            }
        });
    }

    @Override
    public void onSuccess(ArrayList<Student> list) {
        if(type.equals("加载")==true){
            if(list.size()==0){
                showToast("没有更多数据了 〒▽〒");
            }
            listall.addAll(list);
        }else{
            if(listall!=null){
                listall.clear();
                listall.addAll(0,list);
            }
        }
        if(adapter==null) {
            adapter = new Trackadapter(getActivity(), listall);
            listview.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }
        showToast(type+"刷新完成");
        listview.onRefreshComplete();
    }

    @Override
    public void onFail(String info) {
        //失败
        showToast(info);
        listview.onRefreshComplete();
    }

    public void PullDown(){
        if(type.equals("加载")==true) {
            pn++;
        }else{
         pn=1;
        }
        presenter.invalid(3 + "", pn + "");
    }

    public void addList(Student student){
        if (listall!=null&&adapter!=null){
            listall.add(0,student);
            adapter.notifyDataSetChanged();
        }
    }
}
